package com.vinoxm.common.core.handler;

import com.vinoxm.common.base.exception.AuthException;
import com.vinoxm.common.base.exception.BusinessException;
import com.vinoxm.common.base.exception.RssUpdatingException;
import com.vinoxm.common.base.exception.ValidationException;
import com.vinoxm.common.base.result.BaseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

@Log4j2
public class GlobalControllerAdvice extends ResponseBodyHandler {

    public GlobalControllerAdvice() {
        log.info("[GlobalExceptionHandler] Initialization");
    }

//    @ExceptionHandler({Exception.class})
//    @ResponseBody
//    BaseResult handleException(Exception e) {
//        log.error(e);
//        return BaseResult.ServerError();
//    }

    @ExceptionHandler({SQLException.class})
    @ResponseBody
    BaseResult handleSqlException(SQLException e) {
        log.error(e);
        return BaseResult.ServerError();
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    BaseResult handleBusinessException(BusinessException e) {
        log.error("[Business error] " + e.getMessage());
        return BaseResult.BusinessError(e.getMessage());
    }

    @ExceptionHandler(AuthException.class)
    @ResponseBody
    BaseResult handleAuthException(AuthException e) {
        log.error("[Auth error] " + e.getUrl() + " : " + e.getMessage());
        return BaseResult.AuthError(e.getMessage());
    }

    @ExceptionHandler(ValidationException.class)
    @ResponseBody
    BaseResult handleValidationException(ValidationException e) {
        log.error("[Validate error] " + e.getMessage());
        return BaseResult.ValidateError(e.getMessage());
    }

    @ExceptionHandler(RssUpdatingException.class)
    @ResponseBody
    BaseResult handleRssUpdatingException(RssUpdatingException e) {
        log.error("[RSS error] " + e.getMessage());
        return BaseResult.RssUpdatingError();
    }

}
